<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.view' );
require_once  JPATH_ADMINISTRATOR.DS.'includes'.DS.'toolbar.php';
require_once  JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'controllers'.DS.'jausergroup.php';
//require_once  JPATH_SITE.DS.'components'.DS.JBCOMNAME.DS.'views'.DS.'jafrontend_view.php';

// require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'ja_constants.php'); 

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardViewJAProfiles extends JView
{
    /**
     * Display the view
     */
    function display($tmpl = null){
    	$title = getPageTitleFromMenu();
    	$config = new JConfig();
		$document = JFactory::getDocument();
		if( JRequest::getVar("profile_id", '') != ""){
			$this->setLayout('jaview');
		}
		switch ($this->getLayout())
		{
			case 'jalist':
				$this->displayList();				
				if (!$title)
					$title = JText::_('Company');
				$document->setTitle($title.' - '.$config->sitename);
				break;
			case 'jaview':					
				$this->view();
				if (!$title){
					$title = $this->item->company;
					$jobs = $this->item;
					$jobs->title = $this->item->company;
					$jobs->job_detail = "";					    
					if ( ! setPageTitle(array("emp_details_title", "emp_details_keyword", "emp_details_description"), $jobs, $title ) )
						$document->setTitle($title.' - '.$config->sitename);
				}
				
				break;
			case 'jaform':
				$this->edit();
				if (!$title)
					$title = $this->item->company;
				$document->setTitle($title.' - '.$config->sitename);
				break;
			case 'filter_profile':
			default:
				$this->displayList();
				if (!$title){
					$title = JText::_('List of Companys');												    
					if ( ! setPageTitle(array("emp_list_title", "emp_list_keyword", "emp_list_description"), null, $title) )
						$document->setTitle($title.' - '.$config->sitename);
				}
		}
        
        
        parent::display($tmpl);
  	}
  
  	function displayList()
  	{
  		global $mainframe, $option, $jbconfig, $permission;  
  		
  		checkPermission('view_list_profiles');
  		$this->setLayout('jalist');
  		// get data items
        $model = &$this->getModel();   
        
  		$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0 ');
       	$lists = $this->_getViewLists($fieldEditList);  
       	$this->assignRef('lists', $lists);
       	
       	
       	$limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.japrofiles.limit',		'limit',	20,	'int' );
		
        $where_more = ' and a.approved=1 ';
		
        $all_items = $model->getItems($where_more, $limit, $limitstart); 
        $pagination_standard = &$this->get('Pagination');        	       	 
       	//print_r($pagination_standard);exit;
       	$where_more = '';
		if(!$mainframe->isAdmin()){
			$where_more = " and fl.admin_only!=1";
		}
       	$fieldList = $model->getFieldList($where_more);
		$this->assignRef('fieldlist', $fieldList);
		
		$pagination = &$this->get('Pagination');            
        
        $ordering = true;        
		
        $this->assignRef('all_items', $all_items);
       	//$this->assignRef('items_premium', $items_premium);

        $this->assignRef('pagination', $pagination);

        $this->assignRef('ordering', $ordering );

  	}
  	
  	/**
  	* Display edit form
  	*/
  	function edit($postback = false){  		  		        
  		checkPermission('post_profile');
  		
        $user = &JFactory::getUser();
        $model = $this->getModel('japrofiles');
    	$item =$model->getItem($user->id);
    	$temp_user = JFactory::getUser();
    	
        
        // Make sure you are logged in and have the necessary access rights        
        if ( ($item->id && getCurrentUserType() != JBEMPLOYER_USER && $user->id != $item->user_id) || ($user->usertype=='Aministrator')) {
            JResponse::setHeader('HTTP/1.0 403',true);
            JError::raiseWarning( 403, JText::_("Access denied. You don't have permission.") ); 
            echo "<a href='javascript:window.history.go(-1);'>". JText::_('Back')."</a>"; 
            return;
        }
        $postback = JRequest::getVar('postback');
        if($postback){
        	
            $post    = JRequest::get('request', JREQUEST_ALLOWHTML);
            $item->bind($post);
        }
        $this->assignRef('item', $item);
        
        
		$model = $this->getModel('japrofiles');
        $fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
       	$this->assignRef('fieldlist', $fieldEditList);                
        
        if(getCurrentUserType() == JBEMPLOYER_USER && $item->user_id == $user->id){        	
        	$account = getAccountByUserID($item->user_id);
        	$this->assignRef('account', $account);
        }
    }
    
    /**
      * Display view form
      */
    function view($model=null){
		
    	if(!$model) $model = $this->getModel('japrofiles');
    	$user = &JFactory::getUser();
    	
//    	$profile_id = JRequest::getVar('cid', array(0), '', 'array');
//    	$profile_id = $profile_id[0]?(int)$profile_id[0]:0;
//    	if ($profile_id)
//    		$profile_user = getItemByFieldName('ja_profiles','id',$profile_id);
//    	else 
//    		$profile_user = getItemByFieldName('ja_profiles','user_id',$user->id);
//    	if (count($profile_user)>0)
//    	{
//    		$profile_user = $profile_user[0];
//    		$user_id = $profile_user->user_id;
//    	}
//    	else
//    		$user_id = 0;
		$username = JRequest::getVar('cid', array(0), '', 'array');
    	$username = $username[0]?$username[0]:'';
    	
    	if( JRequest::getVar("profile_id", '') != ""){
    		$username = JRequest::getVar("profile_id", '');    		
    	}
    	
    	if( JRequest::getVar("from") == "email"){
    		$profile = getItemByFieldName("ja_profiles", "id", $username);    		
    		$userID  = getItemByFieldName('users','id',"'".$profile->user_id."'");
    		$username = $userID->username;
    	}
    	
    	if ($username)
    	{
    	    $username = str_replace("()", ".", $username);
    		$temp_user = getItemByFieldName('users','username',"'".$username."'");
    		if (isset($temp_user[0]->id))
    			$user_id = $temp_user[0]->id;
    		else 
    			$user_id = 0;
    	}
    	else 	
    		$user_id = $user->id;
        $item = $model->getItem($user_id);
        if ($item->user_id!=$user->id){
        	checkPermission('view_profile_details');
        }
       	$this->assignRef('item', $item);
		
		if(!$item->id){                   
            return;
        } 
       
        if(getCurrentUserType() == JBEMPLOYER_USER && $item->user_id == $user->id){        	
        	$account = getAccountByUserID($item->user_id);
        	$this->assignRef('account', $account);
        }
        
        $fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
        $this->assignRef('fieldlist', $fieldEditList);
        
        $fromsearch = JRequest::getVar('fromsearch');
        $this->assignRef('fromsearch', $fromsearch);
    }
    
    /**
      * Display edit form
      */
    function viewothers(){
        $item = $this->get('ItemView');
        $fieldList = $this->get('EditFieldList');

        $Itemid = JRequest::getVar('Itemid');
        
        $this->setLayout(JBPREFIX."view");
        
        $this->assignRef('toolbar', $toolbar);
        $this->assignRef('item', $item);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('Itemid', $Itemid);
        parent::display();
    }
    
	function &_getViewLists(){
		checkPermission('view_list_profiles');
		global $mainframe, $option, $jbconfig;
		
		$db = &JFactory::getDBO();
		
        /*$lists['filter_status']	= $mainframe->getUserStateFromRequest( $option.".jajobs.filter_status",	'filter_status',		'',	'cmd' );
        if($lists['filter_status']=='') $lists['filter_status'] = -1;*/
		//$lists['filter_order']		= $mainframe->getUserStateFromRequest( $option.".jajobsboard.filter_order",		'filter_order',		"a.{$jbconfig['posts']->get('filter_order', 'is_hotjob')}",	'cmd' );
		///$lists['filter_order_Dir']	= $mainframe->getUserStateFromRequest( $option.".jajobsboard.profiles_order_Dir",	'profiles_order_Dir',	"{$jbconfig['posts']->get('filter_order_Dir', 'DESC')}",	'word' );	
		
		$lists['filter_order']	= $mainframe->getUserStateFromRequest( $option.".japrofiles.filter_order", 'filter_order',	'a.id', 'cmd' );
		$lists['filter_order_Dir']	= $mainframe->getUserStateFromRequest( $option.".japrofiles.filter_order_Dir", 'filter_order_Dir',	'DESC', 'word' );
		//$lists['filter_order_Dir']	= JRequest::getVar('filter_order', 'DESC');
		
		/*
		$search	= $mainframe->getUserStateFromRequest( $option.".jajobs.search", 'search',	'', 'string' );
		$search	= JString::strtolower( $search );        
				
		
		$lists['search']= $search;
		$item = new stdClass();
		
		foreach ($fieldList as $field){
			if($field->is_on_filter){
				$item->{$field->field_name} = $mainframe->getUserStateFromRequest( $option.".jajobs.".$field->field_name,   $field->field_name,   '', 'string' );
				$lists['filter'][$field->field_name] = rendfield($item, $field, true);
			}
		}	
		*/

        // filter_state
        $lists['option']    = $option;

		return $lists;
	}
    
    function getToggleLink( $field_name, $row, $viewonly = true, $imgY = 'tick.png', $imgX = 'publish_x.png', $prefix=''){
        $img     = $row->$field_name ? $imgY : $imgX;
        $task = $row->$field_name ? "inactivate":"activate";
        $alt     = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );
        $action  = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );
        
        if($viewonly){
            $href = '<img src="images/'. $img .'" border="0" alt="'. $alt .'" />';
        } else {
            $href = '
            <a href="javascript:void(0);" onclick="return listItemTask(\''. $row->id .'\',\''. $field_name. '\',\''.$prefix.$task .'\')" title="'. $action .'">
            <img src="images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
            ;
        }

        return $href;
    }
    
    function filter_profile()
    {
    	global $mainframe, $option, $jbconfig, $permission;  
  		
  		checkPermission('view_list_profiles');
  		
  		$model = &$this->getModel();   
        
        
        /* Get field select list */
  		$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0 ');
       	$lists = $this->_getViewLists($fieldEditList);  
       	$this->assignRef('lists', $lists);
  		
       	$limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.japrofiles.limit',		'limit',	20,	'int' );
		
        $where_more = ' and a.approved=1 ';
        
       	// get data items
		$all_items = $model->getitems();
		$this->assignRef('all_items', $all_items);
                
        /* Paging */
       	$pagination = $this->get('Pagination');  //print_r($pagination);exit;
       	$this->assignRef('pagination', $pagination);
        
        /* Get field list */
        $fieldList = $model->getFieldList();       	
        $this->assignRef('fieldlist', $fieldList);		
		                                		
		       
       	$ordering = true;           
        $this->assignRef('ordering', $ordering );  
        $this->setLayout('jalist');
    }
    function getListLimit($limit){
    	$array = array(5, 10, 15, 20, 50, 100);
    	$list_html = array();
    	foreach ($array as $value){
    		$list_html[] = JHTML::_('select.option', $value, $value);  
    	}
    	$list_html = JHTML::_('select.genericlist',   $list_html, 'limit', 'class="inputbox" size="1" onchange="doSearch(this.value)"', 'value', 'text', $limit);
    	return $list_html;
    }
    
}
?>